package com.easytake.emotioncat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easytake.emotioncat.dto.PageResult;
import com.easytake.emotioncat.entity.EmListEntity;
import com.easytake.emotioncat.mapper.EmListMapper;
import com.easytake.emotioncat.service.IEmListService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 情绪记录表 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2022-10-23
 */
@Service
public class EmListServiceImpl extends ServiceImpl<EmListMapper, EmListEntity> implements IEmListService {
    public EmListEntity getByUserAndId(String userId, String emId) {
        QueryWrapper<EmListEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EmListEntity::getUserId, userId).eq(EmListEntity::getId, emId);
        List<EmListEntity> enList = this.list(queryWrapper);
        if (enList.size() > 0) {
            return enList.get(0);
        }
        return null;
    }

    @Override
    public PageResult<EmListEntity> getListByPage(String wx_uid, Integer pageNo, Integer pageSize) {
        EmListMapper mapper = this.getBaseMapper();
        Page<EmListEntity> page = Page.of(pageNo, pageSize);
        QueryWrapper<EmListEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EmListEntity::getUserId, wx_uid);
        OrderItem orderItem = OrderItem.desc("create_time");
        List<OrderItem> orderItemList = new ArrayList<>();
        orderItemList.add(orderItem);
        page.setOrders(orderItemList);
        IPage<EmListEntity> entityIPage = mapper.selectPage(page, queryWrapper);
        PageResult pageResult = new PageResult(entityIPage);
        return pageResult;
    }
}
